﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using BCEntity.OA.Workflow.WorkflowActivityInstanceReference;
using BCCommon;
namespace BCData.OA.Workflow.WorkflowActivityInstanceReference
{
    public class WorkflowActivityInstanceReferenceData : IWorkflowActivityInstanceReferenceData
    {
        private const string QUERY = @"SELECT WorkflowActivityInstanceReferenceNo,WorkflowActivityInstanceId,WorkflowType,BusinessType,ReferenceNo,ReferenceType
                                        FROM OA_WorkflowActivityInstanceReference";

        private readonly IDatabaseContext databaseContext;

        public WorkflowActivityInstanceReferenceData(IDatabaseContext databaseContext)
        {
            this.databaseContext=databaseContext;
        }

        public async Task<bool> Add(WorkflowActivityInstanceReferenceEntity referenceEntity)
        {
            string sql = SqlCommands.GetInsertSql("OA_WorkflowActivityInstanceReference", new string[] { "WorkflowActivityInstanceReferenceNo", "WorkflowActivityInstanceId",
                       "WorkflowType","BusinessType","ReferenceNo","ReferenceType" });
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WorkflowActivityInstanceReferenceNo", referenceEntity.WorkflowActivityInstanceReferenceNo, DbType.AnsiString, size: 50);
            parameters.Add("?WorkflowActivityInstanceId", referenceEntity.WorkflowActivityInstanceId, DbType.Int64, size: 20);
            parameters.Add("?WorkflowType", referenceEntity.WorkflowType, DbType.AnsiString, size: 50);
            parameters.Add("?BusinessType", referenceEntity.BusinessType, DbType.AnsiString, size: 50);
            parameters.Add("?ReferenceNo", referenceEntity.ReferenceNo, DbType.AnsiString, size: 50);
            parameters.Add("?ReferenceType", referenceEntity.ReferenceType, DbType.AnsiString, size: 100);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }

        public async Task<WorkflowActivityInstanceReferenceEntity> Get(long workflowActivityInstanceId)
        {
            string sql = string.Format("{0} WHERE WorkflowActivityInstanceId=?WorkflowActivityInstanceId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WorkflowActivityInstanceId", workflowActivityInstanceId, DbType.Int64, size: 20);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<WorkflowActivityInstanceReferenceEntity>(sql, parameters);
        }

        public async Task<WorkflowActivityInstanceReferenceEntity> Get(string referenceNo)
        {
            string sql = string.Format("{0} WHERE ReferenceNo=?ReferenceNo", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?ReferenceNo", referenceNo, DbType.AnsiString, size: 50);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<WorkflowActivityInstanceReferenceEntity>(sql, parameters);
        }

        public async Task<IEnumerable<WorkflowActivityInstanceReferenceEntity>> GetWorkflowActivityInstanceReferencesByReferenceNumber(string referenceNo)
        {
            string sql = string.Format("{0} WHERE ReferenceNo = ?ReferenceNo", QUERY);

            return await this.databaseContext.Slave.QueryAsync<WorkflowActivityInstanceReferenceEntity>(sql, new { referenceNo });
        }
    }
}
